package cc.messcat.web.map;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.entity.fupin.Company;
import cc.messcat.entity.map.MapPoint;
import cc.messcat.log.OperationLog;
import cc.messcat.service.fupin.CompanyService;
import cc.messcat.service.map.PovertyMapService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 扶贫地图控制层
 */
@Controller
@RequestMapping("/povertyMap")
public class PovertyMapController {
    static Logger log = LoggerFactory.getLogger(PovertyMapController.class);
    @Autowired
    private PovertyMapService povertyMapService;
    @Autowired
    private CompanyService companyService;

    /**
     * 贫困村列表查询
     */
    @OperationLog(desc = "扶贫地图标注点查询", module = "扶贫对象管理")
    @RequestMapping(value = "/queryMarkPoints", method = RequestMethod.GET)
    @ResponseBody
    public Object queryMarkPoints(String pointType) {
        List<MapPoint> mapPoints = null;
        if ("all".equals(pointType)) {
            mapPoints = povertyMapService.retrieveAllObjects();
        } else {
            mapPoints = povertyMapService.queryMapPointsBypPointTypes(pointType.split(","));
        }
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS, mapPoints);
    }

    /**
     * 扶贫地图添加标注点
     */
    @OperationLog(desc = "扶贫地图添加标注点", module = "扶贫对象管理")
    @RequestMapping(value = "/addPoint", method = RequestMethod.POST)
    @ResponseBody
    public Object addPoint(@Valid MapPoint mapPoint) {
        try {
            Long id = povertyMapService.addMapPoint(mapPoint);
            return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS, id);
        } catch (Exception e) {
            return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_ADD_FAIL);
        }
    }

    /**
     * 扶贫地图添加标注点
     */
    @OperationLog(desc = "扶贫地图更新标注点", module = "扶贫对象管理")
    @RequestMapping(value = "/updatePoint", method = RequestMethod.POST)
    @ResponseBody
    public Object updatePoint(@Valid MapPoint mapPoint) {
        try {
            povertyMapService.modifyMapPointBysql(mapPoint);
            return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_UPDATE_SUCCESS);
        } catch (Exception e) {
            return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_UPDATE_FAIL);
        }
    }

    /**
     * 扶贫地图添加标注点
     */
    @OperationLog(desc = "扶贫地图添删除标注点", module = "扶贫对象管理")
    @RequestMapping(value = "/deletePointById", method = RequestMethod.POST)
    @ResponseBody
    public Object deletePointById(Long id) {
        try {
            povertyMapService.removeObject(id);
            return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_SUCCESS);
        } catch (Exception e) {
            return new ResponseBean(CommonConstant.EXCEPTION_CODE_500, CommonConstant.MSG_DELETE_FAIL);
        }
    }

    /**
     * 扶贫地图查询项目库参照信息查询
     *
     * @return
     */
    @OperationLog(desc = "扶贫地图查询项目库参照信息", module = "扶贫对象管理")
    @RequestMapping(value = "/companySelect", method = RequestMethod.GET)
    @ResponseBody
    public Object companySelect(Integer page, Integer limit, String companyName) {
        Pager companyPager = companyService.getCompanyPager(page, limit, companyName, null, null, null, null, null);
        JSONObject jo = new JSONObject();
        jo.put("data", companyPager.getResultList());
        jo.put("count", companyPager.getRowCount());
        jo.put("msg", "");
        jo.put("code", 0);
        return jo;
    }

    @OperationLog(desc = "扶贫地图标注点展示信息", module = "扶贫对象管理")
    @RequestMapping(value = "/mapPointInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object mapPointInfo(Long pointId, String pointType) {
        MapPoint mapPoint = povertyMapService.retrieveObject(pointId);
        Map<String, Object> rs = povertyMapService.queryMapPointInfo(mapPoint.getInfoId(), pointType);
        return rs;
    }

}
